//
//  savingDepositView.m
//  SUZHOUBANK
//
//  Created by apple on 16/4/19.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "savingDepositView.h"
#import <Masonry.h>
#import "BaseAPIManager.h"
#import "savingDeCell.h"
#define API_ACTION_UPDATE_TEMPLATE_LIST @"protocol_template!update.action"
static NSString *kTableViewCellIdentifier = @"CellID";
//储蓄存款
#define API_ACTION_UPDATE_TEMPLATE_SAVINGS_ID @"402886c2401fad1a0140282e801e0042"
@interface savingDepositView() <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIImageView *backgroundImg;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) NSArray *titleArr;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end
@implementation savingDepositView



- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor greenColor];
        [self addSubview:self.backgroundImg];
        [self addSubview:self.btn];
        [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(20);
            make.left.equalTo(self).offset(150);
            make.right.equalTo(self).offset(-150);
            make.height.mas_equalTo(50);
        }];
    }
    return self;
}

- (void)layoutSubviews  {
    
    [super layoutSubviews];
    [self initializeDataSource];
    [self addSubview:self.tableView];
    

}

#pragma mark *** delegate && dataSource ***
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    savingDeCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdentifier];
    if (!cell) {
        cell = [[savingDeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTableViewCellIdentifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.titleLab.text = self.titleArr[indexPath.row];

    
//    [[self.dataSource[0] allKeys] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        if ([obj isEqualToString:self.titleArr[indexPath.row]]) {
//            cell.descLab.frame = CGRectMake(0, 30, cell.bounds.size.width, 70);
////            cell.descLab.frame = CGRectMake(CGRectGetMinX(cell.titleLab.frame), CGRectGetMaxY(cell.titleLab.frame), cell.bounds.size.width, [self sizeWithString:[self.dataSource[0] objectForKey:obj] font:[UIFont boldSystemFontOfSize:15] constainssize:CGSizeMake(cell.bounds.size.width, 1000)].height);
//            cell.descLab.text = [self.dataSource[0] objectForKey:obj];
//        }
//        
//    }];
////    NSLog(@"###%@",[self.dataSource[0] allKeys]);
//    NSLog(@"cell : %@",cell.descLab.text);
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.titleArr.count > 0) {
        return self.titleArr.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.titleArr.count == 0) {
        return 0;
    } else {
        return 100;
    }
}
#pragma mark *** Private ***
- (void)initializeDataSource {
    [BaseAPIManager requestURL:API_ACTION_UPDATE_TEMPLATE_LIST parameters:@{@"id":API_ACTION_UPDATE_TEMPLATE_SAVINGS_ID} completion:^(bool success, NSInteger status, NSDictionary *data, NSError *error) {
        if (success) {
            
//            NSLog(@"data = %@",data);
            
//            NSArray *arr = [NSArray ]
//            NSLog(@"%@",data[@"dic"][0][@"attributeOrder"]);
//            NSLog(@"dataCount : %@",data[@"dic"][0]);
            self.dataSource = [NSMutableArray array];
            self.titleArr = data[@"dic"][0][@"attributeOrder"];
            for (int i = 5; i < [data[@"dic"] count]; i ++) {
                [self.dataSource addObject:data[@"dic"][i][@"protocolData"]];
               
            }
            NSLog(@"da %@",self.dataSource);
  
            if (self.titleArr.count > 0) {
                 [self.tableView reloadData];
                return ;
            }
        }
    }];
    
}
#pragma mark ***  文本自适应 ***
- (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font constainssize:(CGSize)constainsSize {
    CGSize size = CGSizeZero;
    //参数1:设置边界约束
    //参数2:适应可选项
    //参数3:属性描述信息
    //参数4:上下文
    CGRect rect = [string boundingRectWithSize:constainsSize options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:font} context:nil];
    size = rect.size;
    return size;
}
#pragma mark *** Getter ***
- (UIImageView *)backgroundImg {
    
    if (!_backgroundImg) {
        _backgroundImg = [[UIImageView alloc] initWithFrame:self.bounds];
        [_backgroundImg setImage:[UIImage imageNamed:@"储蓄存款背景"]];
    }
    return _backgroundImg;
}

- (UIButton *)btn {
    
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn setBackgroundImage:[UIImage imageNamed:@"储蓄存款_01"] forState:UIControlStateNormal];
    }
    return _btn;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.bounds) + 10, CGRectGetMaxY(self.btn.frame) + 10, CGRectGetWidth(self.bounds) - 20, self.bounds.size.height - (CGRectGetMaxY(self.btn.frame) + 20)) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}
@end